package com.example.contentcenter.service.content;

import com.example.contentcenter.dao.content.ShareMapper;
import com.example.contentcenter.domain.dto.content.ShareDTO;
import com.example.contentcenter.domain.dto.user.UserDTO;
import com.example.contentcenter.domain.entity.content.Share;
import com.example.contentcenter.feignclient.UserCenterFeignClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
public class ShareService {

    private final ShareMapper shareMapper;
    private final UserCenterFeignClient userCenterFeignClient;

    public ShareDTO findById(Integer id) {
        Share share = shareMapper.selectByPrimaryKey(id);
        Integer userId = share.getUserId();
        UserDTO userDTO = this.userCenterFeignClient.findById(userId);
        ShareDTO shareDTO = new ShareDTO();
        BeanUtils.copyProperties(share, shareDTO);
        if(userDTO != null && StringUtils.isNotEmpty(userDTO.getWxNickname())) {
            shareDTO.setWxNickname(userDTO.getWxNickname());
        }
        return shareDTO;
    }


    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<UserDTO> forEntity = restTemplate.getForEntity(
                "http://localhost:8082/users/{id}",
                UserDTO.class, 1
        );
        System.out.println("forEntity.getBody() = " + forEntity.getBody());
        System.out.println("forEntity.getStatusCode() = " + forEntity.getStatusCode());

    }

}
